/**
 * 通用问题模块控制层
 *
 * @author JINMO
 * @since 2022/06/26 10:53
 */

package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.service.admin.AdminIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("admin/issue")
public class AdminIssueController {

    @Autowired
    AdminIssueService adminIssueService;

    /**
     * 通过分页参数查找通用问题并返回
     *
     * @param question  问题
     * @param baseParam 基本参数
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/26 18:54:27
     */
    @RequestMapping(value = "list")
    public BaseRespVo list(String question,BaseParam baseParam) {

        CommonData<MarketIssue> data = adminIssueService.queryCommonIssueByBaseParamAndQuestion(question,baseParam);

        return BaseRespVo.ok(data);
    }

    /**
     * 创建通用问题，保存到数据库
     *
     * @param question 问题
     * @param answer   回答
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/26 17:41:11
     */
    @RequestMapping(value = "create")
    public BaseRespVo create(@RequestBody Map map) {
        String question = (String) map.get("question");
        String answer = (String) map.get("answer");
        if (StringUtils.isEmpty(question.trim()) || StringUtils.isEmpty(answer.trim())) {
            return BaseRespVo.error("问题和答案都不能为空");
        }

        MarketIssue data = adminIssueService.createCommonIssue(question, answer);

        return BaseRespVo.ok(data);
    }

    /**
     * 编辑通用问题
     *
     * @param marketIssue
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/26 18:12:56
     */
    @RequestMapping(value = "update")
    public BaseRespVo update(@RequestBody MarketIssue marketIssue) {
        marketIssue.setQuestion(marketIssue.getQuestion().trim());
        marketIssue.setAnswer(marketIssue.getAnswer().trim());

        if (StringUtils.isEmpty(marketIssue.getQuestion().trim()) || StringUtils.isEmpty(marketIssue.getAnswer().trim())) {
            return BaseRespVo.error("问题和答案都不能为空");
        }

        MarketIssue data = adminIssueService.updateCommonIssue(marketIssue);

        return BaseRespVo.ok(data);
    }

    /**
     * 删除通用问题信息
     *
     * @param marketIssue 市场问题
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/26 18:38:32
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketIssue marketIssue) {
        adminIssueService.deleteCommonIssue(marketIssue);

        return BaseRespVo.ok();
    }
}
